package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.WorkflowRecord;
import com.jhgsys.internal.sypq.service.IWorkflowRecordService;
import com.jhgsys.internal.system.entity.User;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

import static org.apache.shiro.SecurityUtils.getSubject;

/**
 * 项目审批 Controller
 *
 * @author ganjun
 * @date 2024-11-16 21:20:36
 */
@Slf4j
@Validated
@Controller
public class WorkflowRecordController extends BaseController {

    @Autowired
    private IWorkflowRecordService workflowRecordService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowRecord")
    public String workflowRecordIndex(){
        return jumpPage("sypq/workflowRecord/workflowRecordList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowRecord/add")
    public String workflowRecordAdd(){
        return jumpPage("sypq/workflowRecord/workflowRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowRecord/update/{id}")
 //   @RequiresPermissions("workflowRecord:edit")
    public String workflowRecordUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("workflowRecord", workflowRecordService.findWorkflowRecordById(id));
        return jumpPage("sypq/workflowRecord/workflowRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowRecord/audit")
    public String   audit(Model model, @Param("voucherType") String voucherType,@Param("voucherId")String voucherId){
        return jumpPage("sypq/workflowRecord/workflowRecordEdit");
    }
    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-16 21:20:36
     */
    @GetMapping("workflowRecord")
    @ResponseBody
    public FebsResponse getAllWorkflowRecords(WorkflowRecord workflowRecord) {
        try {
            return success(workflowRecordService.findWorkflowRecordList(workflowRecord));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-16 21:20:36
     */
    @GetMapping("workflowRecord/list")
    @ResponseBody
  //  @RequiresPermissions("workflowRecord:view")
    public FebsResponse workflowRecordList(QueryRequest request, WorkflowRecord workflowRecord) {
        try {
            Map<String, Object> dataTable = getDataTable(this.workflowRecordService.findWorkflowRecordPage(request, workflowRecord));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 WorkflowRecord
     * @author ganjun
     * @date 2024-11-16 21:20:36
     */
    @Log("新增WorkflowRecord")
    @PostMapping("workflowRecord")
    @ResponseBody
    public FebsResponse addWorkflowRecord(@Valid WorkflowRecord workflowRecord) throws FebsException {
        try {
            this.workflowRecordService.createWorkflowRecord(workflowRecord);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 WorkflowRecord
     * @author ganjun
     * @date 2024-11-16 21:20:36
     */
    @Log("逻辑删除WorkflowRecord")
    @GetMapping("workflowRecord/delete/{id}")
    @ResponseBody
    public FebsResponse deleteWorkflowRecord(@PathVariable("id") String workflowRecordIds) throws FebsException {
        try {
            WorkflowRecord workflowRecord = new WorkflowRecord();
            workflowRecord.setDeletedBy(getCurrentUser().getUserName());
            workflowRecord.setDeletedTime(new Date());
            workflowRecord.setDeletedFlg(FebsConstant.INVALID);
            this.workflowRecordService.deleteWorkflowRecord(workflowRecord,workflowRecordIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 WorkflowRecord
    * @author ganjun
    * @date 2024-11-16 21:20:36
    */
    @Log("物理删除WorkflowRecord")
    @GetMapping("workflowRecord/physicallyDelete/{id}")
    @ResponseBody
    public FebsResponse physicallyDeleteWorkflowRecord(@PathVariable("id") String workflowRecordIds) throws FebsException {
        try {
            this.workflowRecordService.physicallyDeleteWorkflowRecord(workflowRecordIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 WorkflowRecord
     * @author ganjun
     * @date 2024-11-16 21:20:36
     */
    @Log("修改WorkflowRecord")
    @PostMapping("workflowRecord/update")
    @ResponseBody
    public FebsResponse updateWorkflowRecord(WorkflowRecord workflowRecord) throws FebsException {
        try {
            this.workflowRecordService.updateWorkflowRecord(workflowRecord);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

}
